﻿using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;


namespace Plugins.CodeGeneration
{

    /// <summary>
    /// Mustachio模板引擎，代码生成器
    /// </summary>
    public static class IHostExpand
    {
        /// <summary>
        /// IHost扩展方法
        /// </summary>
        /// <param name="host"></param>
        /// <param name="models"></param>
        /// <param name="replace"></param>
        /// <returns></returns>
        public static IHost Generation(this IHost host, List<string> models,bool replace)
        {
            //多线程
            Thread thread=new Thread(new ThreadStart(ConsoleWriteStart));
            thread.IsBackground = true;
            thread.Start();

            string projectDir = Environment.CurrentDirectory;//获取当前运行绝对路径
            string baseDir = Environment.CurrentDirectory.Replace("\\DotNetCoreApi\\", "\\");//获取项目绝对路径
            string[] projects = new string[] { "DAL", "BLL" };
            string content = "";
            
            foreach (var m in models)
            {
                dynamic model = new ExpandoObject();
                model.entityName = m;
                foreach (var p in projects)
                {
                    #region 接口文件生成 IDAL/IBLL.cs
                    //判断文件是否存在，replace为true，替换文件
                    if (!File.Exists(baseDir + $"\\{p}\\Interface\\I{m}{p}.cs")|| replace)
                    {
                        //读取模板文件，生成代码
                        using (StreamReader sr = new StreamReader(Environment.CurrentDirectory + $"/Plugins/CodeGeneration/TemplateI{p}.txt"))
                        {
                            var template = Mustachio.Parser.Parse(sr.ReadToEnd());
                            // 创建模板数据，也可以是 Dictionary<string,object> 类型的             
                            // 解析获取最终数据
                            content = template(model);
                        }
                        //输出代码文件
                        using (StreamWriter sw = new StreamWriter(baseDir + $"\\{p}\\Interface\\I{m}{p}.cs", false))
                        {
                            //保存数据到文件
                            sw.Write(content);
                        }
                    }
                    #endregion

                    #region 实现文件生成 DAL/BLL.cs
                    //判断文件是否存在
                    if (!File.Exists(baseDir + $"\\{p}\\{m}{p}.cs")|| replace)
                    {
                        //读取模板文件，生成代码
                        using (StreamReader sr = new StreamReader(Environment.CurrentDirectory + $"/Plugins/CodeGeneration/Template{p}.txt"))
                        {
                            var template = Mustachio.Parser.Parse(sr.ReadToEnd());
                            // 创建模板数据，也可以是 Dictionary<string,object> 类型的             
                            // 解析获取最终数据
                            content = template(model);
                        }
                        //写入文件
                        using (StreamWriter sw = new StreamWriter(baseDir + $"\\{p}\\{m}{p}.cs", false))
                        {
                            //保存数据到文件
                            sw.Write(content);
                        }
                    }
                    #endregion

                }
                #region 控制器


                //判断文件是否存在
                if (!File.Exists(baseDir + $"\\DotNetCoreApi\\Controllers\\{m}Controller.cs")|| replace)
                {
                    using (StreamReader sr = new StreamReader(Environment.CurrentDirectory + "/Plugins/CodeGeneration/TemplateController.txt"))
                    {
                        var template = Mustachio.Parser.Parse(sr.ReadToEnd());
                        // 创建模板数据，也可以是 Dictionary<string,object> 类型的             
                        // 解析获取最终数据
                        content = template(model);
                    }
                    using (StreamWriter sw = new StreamWriter(baseDir + $"\\DotNetCoreApi\\Controllers\\{m}Controller.cs", false))
                    {
                        //保存数据到文件
                        sw.Write(content);
                    }
                }
                #endregion

            }
            Console.WriteLine("/// * 执行完毕 * ///");
            return host;
        }
        /// <summary>
        /// 输出开始
        /// </summary>
        public static void ConsoleWriteStart()
        {
            Console.WriteLine("-------------------------------------------------------------------");
            Console.WriteLine("/// * Mustachio模板引擎代码生成器 * ///");
            Console.WriteLine("/// * 执行中... * ///");
        }

    }
}
